using System.Linq;
using RomanJendrusz.Amazon.ETL.Domain;

namespace RomanJendrusz.Amazon.ETL.Repositories
{
    public class SellersRepository: Repository<Seller, string>, ISellersRepository
    {
        public SellersRepository(ISession session)
            : base(session, s => s.Sellers, "SellerId")
        {
        }

        public Seller AttachIfAllreadyExists(Seller item)
        {
            var persistedSeller = session.Sellers.SingleOrDefault(s => s.SellerId == item.SellerId);
            if(persistedSeller == null)
            {
                return item;
            }
            clone(item, persistedSeller);
            return persistedSeller;
        }

        private void clone(Seller attached, Seller orginal)
        {
            if(string.IsNullOrEmpty(attached.GlancePage)== false)
            {
                orginal.GlancePage = attached.GlancePage;
            }
            if (string.IsNullOrEmpty(attached.LegalName) == false)
            {
                orginal.LegalName = attached.LegalName;
            }
            if (string.IsNullOrEmpty(attached.Name) == false)
            {
                orginal.Name = attached.Name;
            }
            if (string.IsNullOrEmpty(attached.NickName) == false)
            {
                orginal.NickName = attached.NickName;
            }
        }
    }
}